package com.leo.service.impl;

import com.leo.base.event.EventBusCenter;
import com.leo.base.event.event.OrderChangeEvent;
import com.leo.base.event.event.OrderCreatedEvent;
import com.leo.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName OrderServiceImpl
 * @Description
 * @Author wangss
 * @date 2020.09.20 20:26
 * @Version 1.0
 */
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Resource
    private EventBusCenter eventBusCenter;

    @Override
    public void createOrder() throws InterruptedException {
        // 创建订单
        // 发送异步事件
        EventBusCenter.postAsync(new OrderCreatedEvent(1L, 1L));
        log.info("发送异步事件,订单创建");
        EventBusCenter.postAsync(new OrderChangeEvent(1L, 1L));
        log.info("发送异步事件,订单修改");

        //发送同步事件
        Thread.sleep(500);
        try {
            log.info("发送同步事件,订单修改，开始");
            EventBusCenter.postSync(new OrderChangeEvent(1L, 1L));
            log.info("发送同步事件,订单修改，结束");
        } catch (Exception e) {
            log.error("发送同步事件,抓异常");
        }
    }
}
